package com.xianyun.site.support.executor.filter.smtp;

import com.xianyun.common.core.filter.Filter;
import com.xianyun.common.core.filter.FilterInvoker;
import com.xianyun.common.redis.service.RedisService;
import com.xianyun.site.support.common.constant.MessageConstants;
import com.xianyun.site.support.common.exception.SmtpErrorCode;
import com.xianyun.site.support.common.exception.SmtpException;
import com.xianyun.site.support.convertor.SmtpConvertor;
import com.xianyun.site.support.domain.model.SmtpMessage;
import com.xianyun.site.support.dto.SmtpReceiveCmd;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author mac_zyj
 *  一分钟内不能重复发送短信规则
 */
@Slf4j
@Component
public class EmailOneMinNotRepeatFilter implements Filter<SmtpReceiveCmd> {


    private static final String CHECK ="校验";
    @Autowired
    private RedisService redisService;



    @Override
    public void doFilter(SmtpReceiveCmd context, FilterInvoker nextFilter) {
        SmtpMessage message = SmtpConvertor.toEntity(context.getSmtpMessage());
        String repeatCheckKey = message.getRedisCheckKey(MessageConstants.REPEAT_CHECK); ;
        if(null!=redisService.getCacheObject(repeatCheckKey) &&
                CHECK.equals(context.getCheckParam().getSystemModel())){
            String errorCode=SmtpErrorCode.EMAIL_ONE_MIN_NOT_REPEAT_ERROR.getErrDesc();
            log.info(SmtpErrorCode.EMAIL_ONE_MIN_NOT_REPEAT_ERROR.getErrDesc()+"-------"+message.getToAddress());
            throw new SmtpException(SmtpErrorCode.EMAIL_ONE_MIN_NOT_REPEAT_ERROR,errorCode);
        }
        nextFilter.invoke(context);
    }
}
